package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Driver;
@Entity
@Table(name="driver_training")
public class Driver_training extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date train_date; //学习日期
	
	public String train_place; //学习地点
	
	public String department; //组织部门
	
	public String form; //组织形式
	
	public String host; //主持人
	
	public Integer expected_attends; //应到人数
	
	public Integer actual_attends; //实到人数
	
	public Integer absence_attends; //缺席人数
	
	public String learning_content; //学习内容
	
	public String feedback; //交流反馈发言
	@ManyToOne
	public Driver driver; //参加驾驶员
	
	public String learning_type; //学习性质
	public static Model.Finder<Integer, Driver_training> find = new Model.Finder<Integer, Driver_training>(Integer.class, Driver_training.class);

	public static Page<Driver_training> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Driver_training> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setTrain_date(Date d) {
		train_date= d;
	}
	public Date getTrain_date() {
		return train_date;
	}
	public void setTrain_place(String d) {
		train_place= d;
	}
	public String getTrain_place() {
		return train_place;
	}
	public void setDepartment(String d) {
		department= d;
	}
	public String getDepartment() {
		return department;
	}
	public void setForm(String d) {
		form= d;
	}
	public String getForm() {
		return form;
	}
	public void setHost(String d) {
		host= d;
	}
	public String getHost() {
		return host;
	}
	public void setExpected_attends(Integer d) {
		expected_attends= d;
	}
	public Integer getExpected_attends() {
		return expected_attends;
	}
	public void setActual_attends(Integer d) {
		actual_attends= d;
	}
	public Integer getActual_attends() {
		return actual_attends;
	}
	public void setAbsence_attends(Integer d) {
		absence_attends= d;
	}
	public Integer getAbsence_attends() {
		return absence_attends;
	}
	public void setLearning_content(String d) {
		learning_content= d;
	}
	public String getLearning_content() {
		return learning_content;
	}
	public void setFeedback(String d) {
		feedback= d;
	}
	public String getFeedback() {
		return feedback;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setLearning_type(String d) {
		learning_type= d;
	}
	public String getLearning_type() {
		return learning_type;
	}
	public static Driver_training findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver_training v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
